package ext.tianma.part.imported;

import java.util.Hashtable;

import wt.enterprise.RevisionControlled;
import wt.fc.PersistenceHelper;
import wt.folder.Folder;
import wt.folder.FolderHelper;
import wt.iba.value.service.LoadValue;
import wt.inf.container.WTContainerRef;
import wt.lifecycle.LifeCycleException;
import wt.lifecycle.LifeCycleHelper;
import wt.lifecycle.State;
import wt.org.WTPrincipalReference;
import wt.part.QuantityUnit;
import wt.part.Source;
import wt.part.WTPart;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.vc.VersionControlHelper;
import wt.vc.Versioned;
import wt.vc.views.View;
import wt.vc.views.ViewException;
import wt.vc.views.ViewHelper;
import wt.vc.views.ViewReference;

public class ImportedPartUtil {		
	
	public static void setPartAttribute( WTPart part , String attributeId , String attributeValue , Hashtable<String,Object> ibaTable , StringBuffer logInfo ) throws WTException {
		ImportedLogUtil.addAttributeInfoLog(attributeId, attributeValue, logInfo);
		
		if( ibaTable == null ){
			ibaTable = new Hashtable<String,Object>() ;
		}
		
		if( part == null ){
			return ;
		}
		
		if( attributeId == null || attributeId.trim().isEmpty()){
			return ;
		}
		
		if( attributeId.equalsIgnoreCase("Type") ){
			setType(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("Number") ){
			setNumber(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("Name") ){
			setName(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("End Item") ){	
			setEndItem(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("Trace Code") ){
			
		}else if( attributeId.equalsIgnoreCase("Generic Type") ){
			
		}else if( attributeId.equalsIgnoreCase("Assembly Mode") ){
			
		}else if( attributeId.equalsIgnoreCase("Location") ){
			setLocation(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("Revision") ){
			
		}else if( attributeId.equalsIgnoreCase("View") ){
			
			// setView(part, attributeValue, logInfo);
			
		}else if( attributeId.equalsIgnoreCase("State") ){
			
			//setState(part, attributeValue, logInfo);
			
		}else if( attributeId.equalsIgnoreCase("Source") ){
			setSource(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("Default Unit") ){
			setDefaultUnit(part, attributeValue, logInfo);
		}else if( attributeId.equalsIgnoreCase("Gathering Part") ){
			
		}else{
			if( attributeId != null && attributeValue != null && !attributeValue.trim().isEmpty() && !attributeId.trim().isEmpty() ){
				ibaTable.put( attributeId , attributeValue );
			}
		}
	}
	
	public static Versioned setCreatorAndModifier(Versioned newVer, WTPrincipalReference principalRef) throws WTException {        
		if( principalRef == null ){
			return newVer ;
		}else{
			boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
			
			try {
				newVer = (Versioned)VersionControlHelper.assignIterationCreator(newVer, principalRef);
				VersionControlHelper.setIterationModifier(newVer, principalRef);
				return newVer;
			} catch(WTPropertyVetoException wpve) {
				throw new WTException(wpve);
			} finally{
				SessionServerHelper.manager.setAccessEnforced(flag);
			}
		}
		
	}
	
	public static String getPartInfo(WTPart part){
		String partInfo = "" ;
		
		if( part != null ){
			partInfo = part.getNumber() + " , " + part.getName() + " , " + part.getViewName() + " , " + getVersion(part) ;
		}
		
		return partInfo ;
	}
	
	/**
	 * 获取对象的大小版本组成的版本序列值，形如： A.1
	 * 
	 * @param revisionControlled
	 * @return
	 */
	public static String getVersion(RevisionControlled revisionControlled){

		String version = "";
		
		if( revisionControlled != null ){
			//获取大版本
			String majorVersion = revisionControlled.getVersionIdentifier().getValue();
			
			
			// 获取小版本
			String minorVersion = revisionControlled.getIterationIdentifier().getValue();

			version = majorVersion + "." + minorVersion;
		}
		
		return version;
	}
	
	public static WTPart tansitView( WTPart part , String newView ){
		boolean flag = SessionServerHelper.manager.setAccessEnforced(false);

		WTPart newViewPart = null ;
		
		try{
			newViewPart=(WTPart) ViewHelper.service.newBranchForView( part , newView );
			newViewPart = (WTPart) PersistenceHelper.manager.save( newViewPart );
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
		} catch (ViewException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}finally{
			SessionServerHelper.manager.setAccessEnforced( flag );
		}
		
		return newViewPart ;
	}
	
	private static void setType(WTPart part, String attributeValue , StringBuffer logInfo) throws WTException {
		logInfo.append(">>>>>> Set Part Type......\n") ;
		if( (attributeValue != null) && (! attributeValue.equals("")) ){
			logInfo.append(">>>>>> Set Part Type : " + attributeValue + " Start......") ;
			
			LoadValue.setType(part, attributeValue);
			
			logInfo.append(">>>>>> Set Part Type : " + attributeValue + " End......") ;
		}else{
			ImportedLogUtil.addErrorTypeInfo( attributeValue , logInfo ) ;
		}
	}
	
	private static void setNumber(WTPart part, String attributeValue, StringBuffer logInfo) {
		if( (attributeValue != null) && (!attributeValue.equals("")) ){
			try {
				String partNumber = part.getNumber() ;
				
				if(partNumber == null || partNumber.equals("")){
					part.setNumber( attributeValue ) ;
				}else{
					ImportedLogUtil.addPartNumberChangeInfo( partNumber , attributeValue, logInfo );
				}
			} catch (WTPropertyVetoException e) {
				ImportedLogUtil.addPartNumberInfoThrowsException( attributeValue, logInfo );
				
				e.printStackTrace();
			}
		}else{
			ImportedLogUtil.addErrorPartNumberInfo(attributeValue, logInfo);
		}
	}

	private static void setName(WTPart part, String attributeValue, StringBuffer logInfo) {
		System.out.println(">>>>>> Set Part Name......") ;
		
		if( (attributeValue != null) && (!attributeValue.equals("")) ){
			String partName = part.getName() ;
			
			try {
				if( partName == null || partName.equals("")){
					System.out.println(">>>>>> Set Part Name : " + attributeValue ) ;
					part.setName( attributeValue ) ;
				}
			} catch (WTPropertyVetoException e) {
				ImportedLogUtil.addPartNameInfoThrowsException( attributeValue, logInfo );
				
				e.printStackTrace();
			}
		}else{
			ImportedLogUtil.addErrorPartNameInfo(attributeValue, logInfo);
		}
	}

	private static void setEndItem(WTPart part, String attributeValue , StringBuffer logInfo) {		
		try{			
			if( (attributeValue != null) && attributeValue.equals("true")){
				part.setEndItem(true) ;
			}else{
				part.setEndItem(false) ;
			}
		} catch (WTPropertyVetoException e) {
			ImportedLogUtil.addPartEndItemInfoThrowsException( attributeValue, logInfo );
			
			e.printStackTrace();
		}
	}

	private static void setLocation(WTPart part , String attributeValue , StringBuffer logInfo) throws WTException {
		if( (attributeValue != null) && (!attributeValue.equals("")) ){
			String partLocation = part.getLocation() ;
			
			if( partLocation == null || partLocation.equals("")){
			
				WTContainerRef containerRef = part.getContainerReference() ;
				
				if ( containerRef != null ) {
					Folder folder = FolderHelper.service.getFolder( attributeValue , containerRef );
					
					if (folder != null) {
						FolderHelper.assignLocation(part, folder);
					}else{
						ImportedLogUtil.addPartFolderErrorInfo( attributeValue, logInfo );
					}
				}else{
					ImportedLogUtil.addPartContainerErrorInfo( attributeValue, logInfo );
				}
			}
		}else{
			ImportedLogUtil.addPartFolderErrorInfo( attributeValue, logInfo );
		}
	}

	public static void setView(WTPart part, String attributeValue, StringBuffer logInfo) throws WTException, ViewException {
		if( ( attributeValue != null ) && ( !attributeValue.equals("") ) ){
			View view  = ViewHelper.service.getView( attributeValue ) ;
			
			if( view != null ){
				ViewReference viewRef = ViewReference.newViewReference(view) ;
				
				part.setView(viewRef) ;
			}else{
				ImportedLogUtil.addErrorViewInfo(attributeValue, logInfo);
			}
		}else{
			ImportedLogUtil.addErrorViewInfo(attributeValue, logInfo);
		}
	}

	public static void setState(WTPart part, String attributeValue,StringBuffer logInfo) throws WTException {
		if( attributeValue == null || attributeValue.trim().equals("")){
			return ;
		}
		
		State state = State.toState(attributeValue.trim()) ;
		
		if( state != null ){
			try {
				LifeCycleHelper.service.setLifeCycleState( part , state );
			} catch (LifeCycleException e) {
				ImportedLogUtil.addStateInfoThrowsException( attributeValue, logInfo );
				
				e.printStackTrace();
			}
		}else{
			ImportedLogUtil.addErrorStateInfo(attributeValue, logInfo);
		}
	}

	private static void setSource(WTPart part, String attributeValue,StringBuffer logInfo) {
		if( (attributeValue != null) && ( !attributeValue.equals("") ) ){
			Source source = Source.toSource( attributeValue ) ;
			
			if( source != null ){
				part.setSource( source ) ;
			}else{
				ImportedLogUtil.addErrorSourceInfo(attributeValue, logInfo);
			}
		}else{
			ImportedLogUtil.addErrorSourceInfo(attributeValue, logInfo);
		}
	}

	private static void setDefaultUnit(WTPart part, String attributeValue,StringBuffer logInfo) {
		if( (attributeValue != null) && ( !attributeValue.equals("") ) ){
			QuantityUnit unit = QuantityUnit.toQuantityUnit( attributeValue );
			
			if( unit != null ){
				try {
					part.setDefaultUnit(unit) ;
				} catch (WTPropertyVetoException e) {
					ImportedLogUtil.addDefaultUnitThrowsException( attributeValue, logInfo );
					
					e.printStackTrace();
				}
			}else{
				ImportedLogUtil.addErrorDefaultUnitInfo(attributeValue, logInfo);
			}
		}else{
			ImportedLogUtil.addErrorDefaultUnitInfo(attributeValue, logInfo);
		}
	}
}
